package com.cmccgz.udacs_landlord_webapp.main.unlock;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.image.GlideUtils;
import com.cmccgz.baselibrary.utils.ScreenUtils;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyGridView;
import com.cmccgz.baselibrary.view.adapter.BoxingAdapter;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.Media;
import com.cmccgz.udacs_landlord_webapp.bean.RecordDetail;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;


public class RecordDetailActivity extends BaseActivity implements View.OnClickListener,RecordDetailContract.View{
    public static final String KEY_REC_ID="key_rec_id";
    private View v_empty;
    private TextView tv_name;
    private TextView tv_id;
    private TextView tv_sex;
    private TextView tv_nation;
    private TextView tv_address;
    private TextView tv_birth;
    private TextView tv_rec_time;
    private TextView tv_house_address;
    private TextView tv_door_card;
    private TextView tv_card_type;
    private ImageView iv_video;
    private MyGridView gv_imgs;

    private RecordDetail currentDetail;
    private String rec_id;
    private RecordDetailPresenter mPresenter;
    private LinearLayout ll_imgs;
    private RelativeLayout rv_gv_video;
    private ImageView iv_start;
    private String videoUrl;
    private String coverUrl;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_record_detail;
    }

    @Override
    protected void initView() {
        ViewHelper viewHelper = ViewHelper.instance(this);
        viewHelper.setTitle("记录详情");
        viewHelper.setAllVisable(false);
        viewHelper.setBackTextVisable(true);

        v_empty = findViewById(R.id.v_empty);
        tv_name = ((TextView) findViewById(R.id.tv_name));
        tv_id = ((TextView) findViewById(R.id.tv_id));
        tv_sex = ((TextView) findViewById(R.id.tv_sex));
        tv_nation = ((TextView) findViewById(R.id.tv_nation));
        tv_address = ((TextView) findViewById(R.id.tv_address));
        tv_birth = ((TextView) findViewById(R.id.tv_birth));
        tv_rec_time = ((TextView) findViewById(R.id.tv_rec_time));
        tv_house_address = ((TextView) findViewById(R.id.tv_house_address));
        tv_door_card = ((TextView) findViewById(R.id.tv_door_card));
        tv_card_type = ((TextView) findViewById(R.id.tv_card_type));
        iv_video = ((ImageView) findViewById(R.id.iv_video));
        gv_imgs = ((MyGridView) findViewById(R.id.gv_imgs));
        ll_imgs = ((LinearLayout) findViewById(R.id.ll_imgs));
        rv_gv_video = ((RelativeLayout) findViewById(R.id.rv_gv_video));
        iv_start = ((ImageView) findViewById(R.id.iv_start));

        v_empty.setOnClickListener(this);
        iv_start.setOnClickListener(this);

        mPresenter = new RecordDetailPresenter(this);

        initDatas();

        getDetail();
    }

    private void getDetail() {
        showLoadingDialog("正在加载记录详情...");
        mPresenter.getDetail(rec_id);
    }

    private void initDatas() {
        rec_id = getIntent().getStringExtra(KEY_REC_ID);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_empty:
                getDetail();
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_start:
                showLoadingDialog("正在加载视频...");
                mPresenter.getVideoUrl(videoUrl);
                break;
        }
    }

    @Override
    public void getDetailSuccess(RecordDetail detail) {
        if (detail==null&&currentDetail==null) {
            v_empty.setVisibility(View.VISIBLE);
        }else{
            v_empty.setVisibility(View.GONE);
            if (detail!=null) {
                //人员信息
                tv_name.setText(StringUtils.isEmpty(detail.name)?"":detail.name);
                tv_id.setText(StringUtils.isEmpty(detail.id_card)?"":detail.id_card);
                tv_sex.setText(StringUtils.isEmpty(detail.sex)?"":detail.sex);
                tv_birth.setText(StringUtils.isEmpty(detail.birth)?"":detail.birth);
                tv_address.setText(StringUtils.isEmpty(detail.address)?"":detail.address);
                tv_nation.setText(StringUtils.isEmpty(detail.nation)?"":detail.nation);
                //刷卡信息
                tv_house_address.setText(StringUtils.isEmpty(detail.house_address)?"":detail.house_address);
                tv_card_type.setText(StringUtils.isEmpty(detail.door_card_type)?"":detail.door_card_type);
                tv_door_card.setText(StringUtils.isEmpty(detail.door_card)?"":detail.door_card);
                tv_rec_time.setText(StringUtils.isEmpty(detail.rec_time)?"":detail.rec_time);

                //...
                List<Media> imgs = detail.imgs;
                if (imgs!=null&& imgs.size()>0) {
                    ll_imgs.setVisibility(View.VISIBLE);
                    List<String>urls=new ArrayList<>();
                    for (Media img : imgs) {
                        String imagePathForRecord = img.getImagePathForRecord(detail.inOutType);
                        Log.e("INX", "getDetailSuccess: imagePathForRecord="+imagePathForRecord );
                        urls.add(imagePathForRecord);
                    }
                    int gv_width = (int) (ScreenUtils.getScreenWidth(mContext) - ScreenUtils.dpToPxInt(mContext, 16 + 16));
                    gv_imgs.setAdapter(new BoxingAdapter(false, gv_imgs, urls, gv_width, this, 4));
                }else{
                    ll_imgs.setVisibility(View.GONE);
                }
//                detail.video="http://183.232.65.75:8765/putFile/putVideo?key=fd903213-b7a0-4fa3-ad18-536776db954b";
                videoUrl = detail.video;
                if (!StringUtils.isEmpty(videoUrl)) {
                    rv_gv_video.setVisibility(View.VISIBLE);
                    coverUrl = new Media(detail.v_cover).getImagePathForRecord(detail.inOutType);
                    GlideUtils.loadImage(mContext, coverUrl,iv_video);
                }else{
                    rv_gv_video.setVisibility(View.GONE);
                }
            }
        }
        dismissLoadingDialog();
    }

    @Override
    public void getDetailError(String msg) {
        v_empty.setVisibility(View.VISIBLE);
        ToastUtils.show(mContext,msg);
        dismissLoadingDialog();
    }

    @Override
    public void getVideoUrlSuccess(String videoUrl) {
        dismissLoadingDialog();
        JZVideoPlayerStandard.startFullscreen(this, JZVideoPlayerStandard.class, videoUrl, "开门视频");
    }

    @Override
    public void getVideoUrlError(String msg) {
        dismissLoadingDialog();
        ToastUtils.show(mContext,msg);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}
